﻿var selectedRowId = { Id: '-1' };
var idsOfSelectedRows = [];
var gridData = '#grdData';

//============================================
$(document).ready(function () {

    tinymce.init({
        mode: "exact",
        elements: "Contents",
        height: 300,
        plugins: [
            "advlist autolink lists link image charmap print preview hr anchor pagebreak",
            "searchreplace wordcount visualblocks visualchars code fullscreen",
            "insertdatetime media nonbreaking save table contextmenu directionality", "emoticons template paste", "textcolor"
    ],
        toolbar1: "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image",
        toolbar2: "print preview media | forecolor backcolor emoticons",
        image_advtab: true
    });

    $(".datepicker").datepicker({
        showOn: "button",
        buttonImage: dateIconURL,
        changeMonth: true,
        changeYear: true,
        buttonImageOnly: true,
        dateFormat: 'mm/dd/yy',
        constrainInput: true
    });

    $('#VisibleContent').attr('checked', true);


    SetupGrid(GetgridOptions(), GetColumnNames, GetColumnModel, selectedRowId, idsOfSelectedRows, FillData, GetDataPosted('#grdData'), null, null);

    $('#btnBrowseImage').click(function () {
        $('#browseImage').click();
    });

    $('#btnRefresh').click(function () {
        //alert(tinymce.get('Contents').getContent());
        ClearText();
    });

    $('#btnFind').click(function () {
        TriggerReloadDataGrid(gridData);
    });

    $('#btnDelete').click(function () {
        DeleteNewsAll();
    });


});

//============================================
function browseImageOnChange() {
    //$('#ImageContentUrl').val($('#browseImage').val());
    var listStr = $('#browseImage').val().split('\\');
    if (listStr.length > 0) {
        $('#ImageContentUrl').val(listStr[listStr.length - 1]);
    }
}

//----------func cleartext()----------------------------------
function ClearText() {
    $('#Condition_Find').val("");
    $('#Subject').val("");
    $('#ImageContentUrl').val("");
    $('#ShortcutText').val("");
    $('#CreationDate').val("");
    tinymce.get('Contents').setContent('<p>Nhập nội dung tin</p>');
    $('#GodTitleID').val("0");
    $('#VisibleContent').attr('checked', true);

}
//------------------------

//========func GetgridOption====================================
function GetgridOptions() {
    var obj = new Object();

    obj.Id = "#grdData";
    obj.Url = GetAllNewsPath;
    obj.Pager = "#pager";
    obj.IsAutoWidthColumn = false;
    obj.IsMultiSelect = false;
    obj.ColumnKeyIndex = "CreationDate";
    obj.DefaultSortName = "CreationDate";
    obj.DefaultSortType = "date";
    obj.DefaultSortOrder = "desc";
    //ClearText();
    /*
    $('#Condition_Find').val("");
    $('#Subject').val("");
    $('#ImageContentUrl').val("");
    $('#ShortcutText').val("");
    $('#CreationDate').val("");
    //tinymce.get('Contents').setContent('<p>Nhập nội dung tin</p>');
    $('#NewsID').val("0");
    */
    return obj;
}
//--------------------------------------------

//============================================
function GetColumnNames() {
    return ['Mã Tin', 'Mã mục tin', 'Mục tin', 'Tiêu đề tin', 'Hình đại diện', 'Tóm tắt nội dung', 'Nội dung tin', 'Hiển thị trang chủ', 'Khóa', 'Ngày tạo'];
}
//--------------------------------------------

//============================================
function GetColumnModel() {
    return [
            { name: 'GodTitleID', index: 'GodTitleID', hidden: true },
            { name: 'GroupID', index: 'GroupID', hidden: true },
            { name: 'GroupName', index: 'GroupName', width: 120, align: "left", sortable: true, searchoptions: { sopt: ['cn', 'eq']} },
            { name: 'Subject', index: 'Subject', width: 200, align: "left", sortable: true, searchoptions: { sopt: ['cn', 'eq']} },
            { name: 'ImageContentUrl', index: 'ImageContentUrl', width: 150, align: "left", sortable: true, searchoptions: { sopt: ['cn', 'eq']} },
            { name: 'ShortcutText', index: 'ShortcutText', width: 350, align: "left", sortable: true, searchoptions: { sopt: ['cn', 'eq']} },
            { name: 'Contents', index: 'Contents', width: 400, align: "left", hidden: true },
            { name: 'VisibleContent', index: 'Contents', width: 50, align: "left", hidden: true },
            { name: 'Locked', index: 'Locked', width: 50, align: "left", sortable: true, searchoptions: { sopt: ['cn', 'eq']} },
            { name: 'CreationDate', index: 'CreationDate', width: 70, align: "left", sortable: true }
            ];
}
//--------------------------------------------
//============================================
function FillData() {

    $('#GodTitleID').val(GetCellValue('#grdData', selectedRowId.Id, 'GodTitleID'));
    $('#GroupID').val(GetCellValue('#grdData', selectedRowId.Id, 'GroupID'));
    $('#GroupName').val(GetCellValue('#grdData', selectedRowId.Id, 'GroupName'));
    $('#Subject').val(GetCellValue('#grdData', selectedRowId.Id, 'Subject'));
    $('#ImageContentUrl').val(GetCellValue('#grdData', selectedRowId.Id, 'ImageContentUrl'));
    $('#ShortcutText').val(GetCellValue('#grdData', selectedRowId.Id, 'ShortcutText'));
    tinymce.get('Contents').setContent(GetCellValue('#grdData', selectedRowId.Id, 'Contents'));
    if (GetCellValue('#grdData', selectedRowId.Id, 'VisibleContent') == "True" || GetCellValue('#grdData', selectedRowId.Id, 'VisibleContent') == "1") {
        $('#VisibleContent').attr('checked', true)
    }
    else {
        $('#VisibleContent').attr('checked', false)
    }

    if (GetCellValue('#grdData', selectedRowId.Id, 'Locked') == "True" || GetCellValue('#grdData', selectedRowId.Id, 'Locked') == "1") {
        $('#Locked').attr('checked', true)
    }
    else {
        $('#Locked').attr('checked', false)
    }

    $('#CreationDate').val(GetCellValue('#grdData', selectedRowId.Id, 'CreationDate'));
    return;
}
//--------------------------------------------
//============================================
function GetDataPosted(list) {

    return {
        Condition_GroupID: function () { return $('#Condition_GroupID').val(); },
        Condition_Find: function () { return $('#Condition_Find').val(); },

        sortName: function () { return $(list).jqGrid('getGridParam', 'sortname'); },
        sortOrder: function () { return $(list).jqGrid('getGridParam', 'sortorder'); },
        rowNumber: function () { return $(list).jqGrid('getGridParam', 'rowNum'); },
        rowId: function () {
            return (updateData == true ? selectedRowId.Id : -1);
        },
        page: function () { return $(list).jqGrid('getGridParam', 'page'); }
    };
}
//--------------------------------------------


function DeleteNewsAll(e) {

    if ($('#GodTitleID').val() == "" || $('#GodTitleID').val() == "0") {
        OpenAlertPopup("Tin tức", "Chưa chọn mục tin để xóa");
        return;
    }

    OpenConfirmPopup("Tin tức", "Bạn có muốn xóa không?", fnDeleteNewsAll);

}

//=======================================
function fnDeleteNewsAll() {

    var opts = new Object();
    opts.ActionUrl = DeleteNewsPath;
    opts.data = { GodTitleID: $('#GodTitleID').val() };
    CallFunctionWithAjaxPostWithContentType(opts, fnDeleteNewsAllSuccessful, null, fnDeleteNewsAllError);
    //clear text
    $('#Condition_Find').val("");
    $('#Subject').val("");
    $('#ImageContentUrl').val("");
    $('#ShortcutText').val("");
    $('#CreationDate').val("");
    tinymce.get('Contents').setContent('<p>Nhập nội dung tin</p>');
    $('#GodTitleID').val("0");
    $('#VisibleContent').attr('checked', true);
}
//---------------------------------------

//=======================================
function fnDeleteNewsAllSuccessful(data) {
    $('#Condition_Find').val("");
    TriggerReloadDataGrid(gridData);
    OpenAlertPopup("Tin tức", data);
}
//---------------------------------------

//=======================================
function fnDeleteNewsAllError(data) {
    $('#Condition_Find').val("");
    TriggerReloadDataGrid(gridData);
    OpenAlertPopup("Tin tức", data);
}
//---------------------------------------
